package com.apexsoft.front.common.utils.httprequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;

/**
 * 输出包
 * @author dinglei
 *
 */
public final class OutPacket{
	
	private JSONObject data;
	
	public OutPacket(){
		this.data = new JSONObject();
		data.put(Result.CODE, Result.EC_1000);
		data.put(Result.NOTE, "");
	}
	
	public OutPacket(JSONObject json){
		this.data = json;
	}
	
	public OutPacket(int code){
		this();
		data.put(Result.CODE, code);
	}
	
	public OutPacket(int code,String note){
		this(code);
		data.put(Result.NOTE, note);
	}
	
	public OutPacket(String jsonStr){
		this.data = JSON.parseObject(jsonStr);
	}
	//----------------------------------------------------------
	
	public OutPacket set(Object key,Object value){	
		data.put(key.toString(), value);
		return this;
	}
	
	public Object get(Object key){
		return data.get(key);
	}
	
	public OutPacket setRecordCount(int count){
		data.put(Result.COUNT, count);
		return this;
	}
	
	public int getRecordCount(){
		return data.getIntValue(Result.COUNT);
	}
	
	public OutPacket setRecordTotal(int total){
		data.put("totalCount", total);
		return this;
	}
	
	public int getRecordTotal(){
		return data.getIntValue("totalCount");
	}
	
	public OutPacket setObject(JSONObject obj){
		data.put(Result.OBJECT, obj);
		return this;
	}
	
	public JSONObject getObject(){
		return data.getJSONObject(Result.OBJECT);
	}
	
	public OutPacket setRecords(JSONArray records){
		data.put(Result.RECORDS, records);
		if(records!=null)
			this.setRecordCount(records.size());
		else
			this.setRecordCount(0);
		return this;
	}
	
	public OutPacket setRecords(String jsonArrayStr){
		JSONArray records = JSON.parseArray(jsonArrayStr);
		return this.setRecords(records);
	}
	
	public JSONArray getRecords(){
		if(!data.containsKey(Result.RECORDS)){
			JSONArray records = new JSONArray();
			data.put(Result.RECORDS, records);
		}
		return data.getJSONArray(Result.RECORDS);
	}
	
	public void addRecord(Object item){
		this.getRecords().add(item);
		this.setRecordCount(data.getJSONArray(Result.RECORDS).size());
	}
	
	public OutPacket setNote(String note){
		data.put(Result.NOTE, note);
		return this;
	}
	
	public String getNote(){
		return data.getString(Result.NOTE);
	}
	
	public OutPacket setCode(int code){
		data.put(Result.CODE, code);
		return this;
	}
	
	public int getCode(){
		if(data.containsKey(Result.CODE))
			return data.getIntValue(Result.CODE);
		else
			return -1;
	}
	
	public OutPacket setErrcode(String errcode){
		data.put("errcode", errcode);
		return this;
	}
	
	public OutPacket setErrcode(String menu_code,String sub_errcode){
		data.put("errcode", menu_code+sub_errcode);
		return this;
	}
	
	public String getErrcode(){
		return data.getString("errcode");
	}
	
	public String getString(String key){
		return this.data.getString(key);
	}
	
	public String getString(String key,String defaultValue){
		if(this.data.containsKey(key))
			return this.data.getString(key);
		else
			return defaultValue;
	}
	
	public int getInt(String key){
		return this.data.getIntValue(key);
	}
	
	public int getInt(String key,int defaultValue){
		if(this.data.containsKey(key))
			return this.data.getIntValue(key);
		else
			return defaultValue;
	}
	
	public double getDouble(String key){
		return this.data.getDoubleValue(key);
	}
	
	public double getDouble(String key,double defaultValue){
		if(this.data.containsKey(key))
			return this.data.getDoubleValue(key);
		else
			return defaultValue;
	}
	
	public boolean getBoolean(String key){
		return this.data.getBooleanValue(key);
	}
	
	public boolean getBoolean(String key,boolean defaultValue){
		if(this.data.containsKey(key))
			return this.data.getBooleanValue(key);
		else
			return defaultValue;
	}
	
	public JSONObject getJSONObject(String key){
		return this.data.getJSONObject(key);
	}
	
	public JSONArray getJSONArray(String key){
		return this.data.getJSONArray(key);
	}
	
	public long getLong(String key){
		return this.data.getLongValue(key);
	}
	
	public long getLong(String key,long defaultValue){
		if(this.data.containsKey(key))
			return this.data.getLongValue(key);
		else
			return defaultValue;
	}
	
	public String toString(){
		return data.toJSONString();
	}
	
	public JSONObject toJson(){
		return this.data;
	}
	
	public void remove(String key){
		this.data.remove(key);
	}

}
